package cn.codesea.housework_user.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import cn.codesea.housework_user.utils.SPUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AcActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.join_tv)
    TextView join_tv;

    Activity act;

    @BindView(R.id.actName_tv)
    TextView actName_tv;
    @BindView(R.id.actTime_tv)
    TextView actTime_tv;
    @BindView(R.id.actAddress_tv)
    TextView actAddress_tv;
    @BindView(R.id.actNum_tv)
    TextView actNum_tv;
    @BindView(R.id.actPrice_tv)
    TextView actPrice_tv;
    @BindView(R.id.actDesc_tv)
    TextView actDesc_tv;

    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ac);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initData();
        initView();
    }

    public void initData(){
        act = (Activity) getIntent().getSerializableExtra("act");
        if(SPUtil.isLogin(AcActivity.this)) {
            user = new Gson().fromJson(SPUtil.get(AcActivity.this,SPUtil.USER,""),User.class);

            ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
            Call<Result> call = activityService.actIsJoin(act.getActivityid(),user.getUserid());
            call.enqueue(new Callback<Result>(){
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    Result result = response.body();
                    if (200 == result.getState()){
                        // 已报名
                        join_tv.setClickable(false);
                        join_tv.setText("已报名");
                        join_tv.setBackgroundColor(getResources().getColor(R.color.grey_dark));
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {

                }
            });
        }


    }

    public void initView(){
        initToolbar();

        if (act != null){
            actName_tv.setText(act.getName());
            actTime_tv.setText(act.getTime());
            actAddress_tv.setText(act.getAdress());
            actNum_tv.setText(act.getNum()+"");
            actPrice_tv.setText(act.getPrice()+"/人");
            actDesc_tv.setText(act.getDesc());
        }
    }

    public void initToolbar(){
        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.join_tv)
    public void join_tv(){

        if (user == null){
            Dialog dialog = new AlertDialog.Builder(AcActivity.this)
                    .setTitle("提示")
                    .setMessage("请先登录！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(AcActivity.this,LoginActivity.class));
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
            return;
        }

        if (0 == user.getType()){
            // 子女
            startActivity(new Intent(AcActivity.this,JHListActivity.class).putExtra("userid",user.getUserid()).putExtra("type","1").putExtra("act",act));
        }else {
            // 老人
            ActivityService activityService = ServiceGenerator.createService(ActivityService.class);
            Call<Result> call = activityService.joinActs(act.getActivityid(),user.getUserid());
            call.enqueue(new Callback<Result>(){
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    resultProcess(response.body(),"报名");
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {

                }
            });
        }

    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result,String msg){
        if (result.getState() == 200){
            Dialog dialog = new AlertDialog.Builder(AcActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "成功！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(AcActivity.this,MainActivity.class));
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }else {
            Dialog dialog = new AlertDialog.Builder(AcActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "失败！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }
    }
}
